import functions from './functions.js';
var config = require('./config.js');
function request(url, param = {}, way = 'POST',succCallback, errorCallback,contenttype=true) {
  let authToken = wx.getStorageSync('authToken');
  param.time = (new Date()).getTime();
  let header = {};
  if(contenttype==true){
    header = {
      "content-type":'application/x-www-form-urlencoded',
      "Authorization":authToken
    }
  }else{
    header = {
        "content-type":'application/json;charset=UTF-8',
        "Authorization":authToken
    }
  }
  let data = {};
  if (param) {
      for (let key in param) {//如果传来的参数为null，就将这个属性删除
          if (param[key] == null || param[key] == 'null') {
              delete param[key]
          }
      }
      data = {...data, ...param}
  }
  wx.request({
    url: config.API_URL + url,
    data: data,
    header: header,
    method: way,
    success: (response) => {
      let result = response.data;
      if (result.success == true) {
        succCallback(result);
      } else if (result.code == -2) {
        // 登录失效后跳转至登录页面
        wx.setStorageSync('authToken','')
        functions.checkSession();
      } else if (result.code == 500 && result.msg == "团长已经关闭") { 
        succCallback(result)
      }
      else {
        errorCallback(result);
      }
    },
    fail: (error) => {
      functions.showToast('数据加载异常!');
    },
    complete: (e) => {
    }

  });
}
export default request
